#!/usr/bin/python 
#-*- coding=utf-8 -*-

import logging
from ConfigParser import ConfigParser
from server import Server

class Broker():
    def __init__(self, conf_path):
        try:
            self.conf = ConfigParser()
            self.conf.read(conf_path)
        except Exception, e:
            print 'broker init conf failed, %s' % str(e)

        # init logger
        log_level = {}
        log_level['DEBUG'] = logging.DEBUG
        log_level['INFO'] = logging.INFO
        log_level['ERROR'] = logging.ERROR
        try:
            loglevel = log_level[self.conf.get('all', 'log-level')]
        except Exception, e:
            loglevel = logging.DEBUG

        formatstr = '%(asctime)s %(name)-8s %(levelname)-8s %(message)s'
        logging.basicConfig(level = loglevel, format = formatstr)
        self.logger = logging.getLogger('broker')
        self.logger.info('logger init OK.')

    def run(self):
        server = Server(self.conf)
        server.start()

if __name__ == '__main__':
        broker = Broker('./conf/broker.conf')
        broker.run()
